$(function(){
	loadData();
});
function queryData(){
	//分页上面的查询，必须重新赋值pageIndex=1
	$("#pageIndex").html("1");
}

function loadData() {
	$("#dataList").html("");
	var pageIndex = $("#pageIndex").html();
	var action="/canxunSystem/selectMenuForPage";
	ajaxWeb(action,{"page" : pageIndex},function(data) {
	if(data.code=="40000"){//有缓存
		var rows = data.resobj.rows;
		var sss = "";
		$.each(rows,function(index, value) {
			sss+="<tr id="+rows[index].MENU_ID+" style=\"waa\">";
			sss+="<td>"+parseInt(index+1)+"</td>";
			sss+="<td>"+rows[index].MENU_NAME+"</td>";
			sss+="<td>"+rows[index].MENU_URL+"</td>";
			sss+="<td>"+rows[index].MENU_ORDER+"</td>";
			sss+="<td><a class=\"btn btn-success\" href=\"javascript:void(0)\" onclick=\"spreadMenu('"+rows[index].MENU_ID+"');\">" +
					"<i class=\"glyphicon glyphicon-zoom-in icon-white\" id=\""+rows[index].MENU_ID+"_button\">展开</i></a>";
			sss+="&nbsp;&nbsp;<a class=\"btn btn-info\" href=\"#\"><i class=\"glyphicon glyphicon-edit icon-white\"></i> 编辑 </a>";
			sss+="&nbsp;&nbsp;<a class=\"btn btn-danger\" href=\"#\"> <i class=\"glyphicon glyphicon-trash icon-white\"></i> 删除 </a></td>";
			sss+="</tr>";
		});
		$("#dataList").append(sss);
	}
	}, 'json');
}

var ssq="";
//展示二级菜单
function spreadMenu(MENU_ID){
	var ssa = $("#"+MENU_ID).attr('style');
	if(ssa.indexOf("waa")!=-1){//有
		//在后面追加菜单，并展示
		var action="/canxunSystem/selectMenuByParentId?menuId="+MENU_ID;
		ajaxWeb(action,null,function(data) {
		if(data.code=="40000"){//有缓存
			$("#"+MENU_ID+"_button").html("折叠");
			var rows = data.resobj;
			var sss = "";
			ssq = rows.length;
			$.each(rows,function(index, value) {
				sss+="<tr id=\""+rows[index].PARENT_ID+"_"+parseInt(index)+"\">";
				sss+="<td></td>";
				sss+="<td>"+rows[index].MENU_NAME+"</td>";
				sss+="<td>"+rows[index].MENU_URL+"</td>";
				sss+="<td>"+rows[index].MENU_ORDER+"</td>";
				sss+="<td><a class=\"btn btn-success\" href=\"javascript:void(0)\" onclick=\"editMenu('"+rows[index].MENU_ID+"');\">" +
						"<i class=\"glyphicon glyphicon-edit icon-white\"></i>编辑 </a>";
				sss+="&nbsp;&nbsp;<a class=\"btn btn-danger\" href=\"javascript:void(0)\" onclick=\"delMenu('"+rows[index].MENU_ID+"');\">" +
						"<i class=\"glyphicon glyphicon-trash icon-white\">&nbsp;删除</i>  </a></td>";
				sss+="</tr>";
			});
			$("#"+MENU_ID).after(sss);
			$("#"+MENU_ID).attr('style',"wbb");
		}
		}, 'json');
	}else{//没有
		//隐藏它后面的菜单
		for(var j=0;j<ssq;j++){
			$("#"+MENU_ID+"_"+j).hide();
		}
		$("#"+MENU_ID).attr('style',"waa");
		$("#"+MENU_ID+"_button").html("展开");
	}
}

function addData(){
	window.location.href="menu_add.html";
}
function editMenu(menuid){
	window.location.href="menu_edit.html?menuid="+menuid;
}
function delMenu(MENU_ID){
	var action="/canxunSystem/shanchuMenu?MENU_ID="+MENU_ID;
	ajaxWeb(action,null,function(data) {
		loadData();
	});
}